package com.recSys.dataPrediction;

import com.recSys.model.BusiProcBaseInfo;
import com.recSys.util.HuiMinType;
import com.recSys.util.ResourcePathHandler;
import de.bwaldvogel.liblinear.Feature;
import de.bwaldvogel.liblinear.FeatureNode;
import de.bwaldvogel.liblinear.Linear;
import de.bwaldvogel.liblinear.Model;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by dell on 2018/1/29.
 */
public class Predict4Ceme {

    static final String rootPath = ResourcePathHandler.getProgrameRootPath();
    static final String filePath4Fun_model = rootPath + "\\data\\model\\recCeme_model.txt";


    public static double predict (Map<String,Integer> map, BusiProcBaseInfo bpInfo) {
        try {
            int[] hmType = new HuiMinType().getHmType();
//            Feature[] instance = new Feature[10];
            List<Feature> list = new ArrayList<>();
            list.add(new FeatureNode(map.get("c_PosID"), 1));
            list.add(new FeatureNode(4729,map.get("c_age")/100.0));
            if(map.get("c_sex") == 1)
                list.add(new FeatureNode(4730, 1));
            else
                list.add(new FeatureNode(4731, 1));
            if(map.get("d_sex") == 1)
                list.add(new FeatureNode(4732, 1));
            else
                list.add(new FeatureNode(4733, 1));
            list.add(new FeatureNode(4734,map.get("d_age")/100.0));
            list.add(new FeatureNode(4734 + map.get("d_PosID"), 1));
            list.add(new FeatureNode(9462 + hmType[bpInfo.getDeadHMPolicyType()], 1));
            if(bpInfo.getDeadEthnic() != null)
                list.add(new FeatureNode(9475 + bpInfo.getDeadEthnic(), 1));
            if(bpInfo.getDeadReligion() != null)
                list.add(new FeatureNode(9534 + bpInfo.getDeadReligion(), 1));
            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(filePath4Fun_model))));
            Model model = Linear.loadModel(br);
            br.close();
            Feature[] array = new Feature[list.size()];
            return Linear.predict(model, list.toArray(array));
        } catch (Exception e) {
            System.out.println(e.getStackTrace());
            return -1.0;
        }
    }
}
